package com.spj.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //重写认证的方法
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll()
        .antMatchers("/level1/**").hasRole("vip1")
        .antMatchers("/level2/**").hasRole("vip2")
        .antMatchers("/level3/**").hasRole("vip3");

        http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login");
        //防止网站攻击
        http.csrf().disable();
        //注销
        http.logout().logoutSuccessUrl("/");
        //记住我功能实现
        http.rememberMe();

    }

    //重写授权的方法

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

       auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
               .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
               .and().withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3");

    }
}